//
//  FinancialPersonalVC.m
//  BTCoin
//
//  Created by Robin on 28/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "FinancialPersonalVC.h"
#import "FinancialService.h"
#import "FinancialPersonalTableCell.h"
#import "FinancialPersonalModel.h"
#import "FVirtualWalletModel.h"

@interface FinancialPersonalVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UIScrollView * scrollView;
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) UILabel * emptyLabel;
@property(nonatomic,strong) FinancialPersonalModel * personalModel;
@property(nonatomic,strong) NSMutableArray * recordArray;
@end

@implementation FinancialPersonalVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleText:@"资产记录"];
    [self initUIHead];
    [self requestPersonalRecords];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUIHead {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = UIColorFromRGB(0xEFEFEF);
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.top.equalTo(self.headView.mas_bottom);
    }];
    
    UIView * scrollContainerView = [[UIView alloc] init];
    [self.scrollView addSubview:scrollContainerView];
    [scrollContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.scrollView);
        make.bottom.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    
    UIView * titleContainerView = [[UIView alloc] init];
    titleContainerView.backgroundColor = [UIColor whiteColor];
    [scrollContainerView addSubview:titleContainerView];
    [titleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.scrollView);
        make.height.mas_equalTo(40.0);
    }];
    
    NSArray * titleArray = @[@"币种名称",@"可用资产",@"冻结资产",@"总量"];
    for (int i = 0; i < titleArray.count; i++) {
        UILabel *label = [UILabel new];
        label.backgroundColor = [UIColor clearColor];
        label.text = titleArray[i];
        label.tag = i+1;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        [titleContainerView addSubview:label];
        
        if (0 == i) {
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.centerY.equalTo(titleContainerView);
                make.width.mas_equalTo(90.0);
            }];
        } else if (titleArray.count == i+1) {
            UILabel * pLabel = (UILabel *)[titleContainerView viewWithTag:i];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(pLabel.mas_trailing);
                make.centerY.trailing.equalTo(titleContainerView);
                make.width.equalTo(pLabel);
            }];
        } else {
            UILabel * pLabel = (UILabel *)[titleContainerView viewWithTag:i];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(pLabel.mas_trailing).offset(5);
                make.centerY.equalTo(titleContainerView);
                make.width.equalTo(pLabel);
            }];
        }
    }
    
    self.emptyLabel = [[UILabel alloc] init];
    self.emptyLabel.textColor = color_333333;
    self.emptyLabel.text = @"暂无相关数据";
    self.emptyLabel.font = [UIFont systemFontOfSize:17.0];
    [scrollContainerView addSubview:self.emptyLabel];
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(scrollContainerView);
    }];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"FinancialPersonalTableCell" bundle:nil] forCellReuseIdentifier:@"FinancialPersonalTableCell"];
    [scrollContainerView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(scrollContainerView);
        make.top.equalTo(titleContainerView.mas_bottom);
    }];
}

- (void)requestPersonalRecords {
    Loading_Pig_Show(self.view);
    [[FinancialService sharedFinancialService] financialIndex:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.personalModel = [[FinancialPersonalModel alloc] initWithDictionary:responseObject error:nil];
            [self updateUI];
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

- (void)updateUI {
    self.recordArray = [[NSMutableArray alloc] init];
    NSDictionary * dict = self.personalModel.fvirtualwallets[@"1"];
    if (dict) {
        [self.recordArray addObject:dict];
    }
    
    dict = self.personalModel.fvirtualwallets[@"12"];
    if (dict) {
        [self.recordArray addObject:dict];
    }
    
    dict = self.personalModel.fvirtualwallets[@"13"];
    if (dict) {
        [self.recordArray addObject:dict];
    }
    
    dict = self.personalModel.fvirtualwallets[@"14"];
    if (dict) {
        [self.recordArray addObject:dict];
    }
    
    self.tableView.hidden = 0 == [self.personalModel.fvirtualwallets allKeys].count;
    [self.tableView reloadData];
}

- (NSString *)getCoinName:(NSString*)coinType {
    if ([coinType isEqualToString:@"BTC"]) {
        return @"比特币";
    } else if ([coinType isEqualToString:@"LTC"]) {
        return @"莱特币";
    } if ([coinType isEqualToString:@"ETH"]) {
        return @"以太币";
    } if ([coinType isEqualToString:@"ETC"]) {
        return @"以太经典";
    }
    
    return @"";
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.recordArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ChannelIdentifier = @"FinancialPersonalTableCell";
    FinancialPersonalTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ChannelIdentifier forIndexPath:indexPath];
    
    [self configCell:cell indexPath:indexPath];
    
    return cell;
}

- (void)configCell:(FinancialPersonalTableCell *)cell indexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = self.recordArray[indexPath.row];
    FVirtualWalletModel * model = [[FVirtualWalletModel alloc] initWithDictionary:dic error:nil];
    cell.typeLabel.text = [self getCoinName:model.fvirtualcointype.fShortName];
    cell.freezeLabel.text = [NSString stringWithFormat:@"%.4f",model.ffrozen];
    cell.totalLabel.text = [NSString stringWithFormat:@"%.4f",model.ftotal];
    cell.usableLabel.text = [NSString stringWithFormat:@"%.4f",model.ftotal-model.ffrozen];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40.0;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
